package com.lywz.web.action.admin.food;

import java.io.File;
import java.util.List;

import com.lywz.biz.common.entity.goodfood.ClassicFood;
import com.lywz.core.exception.SSException;
import com.lywz.core.fileUpload.UploadFile;
import com.lywz.core.log.LogClerk;
import com.lywz.core.web.WebConstants;
import com.lywz.web.struts.GZAbstracterAction;

public class ClassicFoodBackOperateAction extends GZAbstracterAction {

    private static final long serialVersionUID = 4847425105324421351L;

    private List<ClassicFood> classicFoodList;

    private ClassicFood classicFood;

    private int classicFoodId;
    
    //列表中显示的图片
    private File MainImageFile;

     //经典美食个体页面显示的图片文件
    private File MinorImageFile;
    
    //主图片文件名称
    private String mainImageName;
    
    //次图片文件名称
    private String minorImageName;
    
    /**
     * 获取经典美食的列表并且按照权重排序，跳转到list页面
     * 
     * @return
     */
    public String getClassicFoodListByPower() {
        try {
            classicFoodList = classicFoodService.getAllClassicFoodList();
            return "toClassicFoodList";
        } catch (SSException e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }

    }

    /**
     * 根据id删除一个classicFood
     * 
     * @return
     */
    public String deleteOneClassicFood() {
        try {
            if (classicFoodService.deleteOneClassicFoodById(classicFoodId)) return "actionToClassicFoodList";
            else return ERROR;
        } catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }

    }

    /**
     * 跳转到修改美食页面
     * 
     * @return
     */
    public String toUpdateClassicFoodPage() {
        try {
            classicFood = classicFoodService.getOneClassicFoodById(classicFoodId);
            classicFoodList = classicFoodService.getTopTenList(10);
            return "toUpdateClassicFoodPage";
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
    }
    
    /**
     * @param classicFood the classicFood to set
     */
    public void setClassicFood(ClassicFood classicFood) {
        this.classicFood = classicFood;
    }

    /**
     * 
     * 修改一个经典菜肴完毕 跳转到列表
     * @return
     */
    public String updateClassicFood(){
        String mainImage = "";
        String minorImage = "";
        try{
            mainImage = saveImageFile(MainImageFile, WebConstants.foodImagePath, mainImageName, 0, 0);
            minorImage = saveImageFile(MinorImageFile, WebConstants.foodImagePath, minorImageName, 0, 0);
        }
        catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
        classicFood.setImageMainUrl(mainImage);
        classicFood.setImageMinorUrl(minorImage);
        try{
            if(classicFoodService.updateClassicFood(classicFood))
                
                return "actionToClassicFoodList";
            else
                return ERROR;
        }
        catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }
    
    /**
     * 添加一个新的经典菜肴
     * @return
     */
    public String addNewClassicFood(){
    
            String mainImage;
            String minorImage;
            try{
                mainImage = saveImageFile(MainImageFile, WebConstants.foodImagePath, mainImageName  , 0, 0);
                minorImage = saveImageFile(MinorImageFile, WebConstants.foodImagePath, minorImageName, 0, 0);
            }catch (Exception e) {
                LogClerk.sysout.error(e);
                return ERROR;
            }
            classicFood.setImageMainUrl(mainImage);
            classicFood.setImageMinorUrl(minorImage);
            try{  
            if(classicFoodService.addNewClassicFood(classicFood))
                return "actionToClassicFoodList";
            else
                return ERROR;
        }catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
        
    }
    
    /**
     * 跳转到经典菜肴具体信息
     * @return
     */
    public String toShowClassicFoodPage() {
        try {
            classicFood = classicFoodService.getOneClassicFoodById(classicFoodId);
            return "toShowClassicFood";
        } catch (Exception e) {
            LogClerk.sysout.error(e);
            return ERROR;
        }
    }
    
    /**
     * 跳转到添加页面
     * @return
     */
    public String toAddNewClassicFoodPage(){
        return "toAddClassicFood";
    }

    
    /**
     * 保存上传图片并返回路径
     * 
     * @param imageFile
     * @param dir
     * @param imageFileFileName
     * @param imageWith
     * @param imageHeight
     * @return
     * @throws SSException
     */
    public String saveImageFile(File imageFile, String dir, String imageFileFileName,
            int imageWith, int imageHeight) throws SSException {

        UploadFile files = new UploadFile(imageFile, dir, imageFileFileName, imageWith,
                imageHeight, request);

        String path = defaultFileUpload.uploadOneFile(files);

        return path;
    }
    ////////////////////////////////////$$$$$$$$$$getter setter$$$$$$$$$/////////////////////
    /**
     * @return the classicFoodId
     */
    public int getClassicFoodId() {
        return classicFoodId;
    }

    /**
     * @param classicFoodId the classicFoodId to set
     */
    public void setClassicFoodId(int classicFoodId) {
        this.classicFoodId = classicFoodId;
    }

    /**
     * @return the classicFoodList
     */
    public List<ClassicFood> getClassicFoodList() {
        return classicFoodList;
    }

    /**
     * @return the classicFood
     */
    public ClassicFood getClassicFood() {
        return classicFood;
    }

    
    /**
     * @return the mainImageFile
     */
    public File getMainImageFile() {
        return MainImageFile;
    }

    
    /**
     * @return the minorImageFile
     */
    public File getMinorImageFile() {
        return MinorImageFile;
    }

    
    /**
     * @param mainImageFile the mainImageFile to set
     */
    public void setMainImageFile(File mainImageFile) {
        MainImageFile = mainImageFile;
    }

    
    /**
     * @param minorImageFile the minorImageFile to set
     */
    public void setMinorImageFile(File minorImageFile) {
        MinorImageFile = minorImageFile;
    }
    

}
